export default {
	data() {
		return {

		}
	},
	mounted() {

	},
	beforeDestroy() {

	},
	activated() {

	},
	deactivated() {

	},
	methods: {
		questionStatus(item, index) {
			var className = ''
			if(item.isCorrect == '1') {
				className = 'success'
			}
			if(item.isCorrect == '2') {
				className = 'error'
			}
			if(this.viewIndex == index && this.viewType == item.questionType) {
				className += ' doing'
			}
			if(item.overed) {
				className += ' overed'
			}
			return className
		},
		getArray(questionType) {
			return this[questionType];
		},
		closeOpen() {
			this.topicList.map((item, index) => {
				item.show = false
			})
		},
		selectionChange(selections) {
			this.selections = selections;
		},
		openSelect() {
			this.reGet();
			this.selectShow = true;
		},
		clearSeCheck(flag) {
			this.selections = [];
			this.selectShow = false;
			this.$refs.topicList.clearSelection()
		},
		beforeSeCheck() {
			var selections = this.selections;
			if(selections && selections.length > 0) {
				this.addLoading = true;
				this.selectionIndex = 0;
				this.getAndAdd();
			} else {
				this.$message({
					type: 'error',
					message: '请选择试题'
				})
			}
		},
		nextAdd() {
			if(this.selectionIndex < this.selections.length - 1) {
				this.selectionIndex++;
				this.getAndAdd();
			} else {
				this.selections = [];
				this.selectionIndex = 0;
				this.selectShow = false;
				this.addLoading = false;
				this.sumSelection();
			}
		},
		openList(item, index) {
			this.clearCheck();
			this.showForm = false;
			this.viewIndex = index;
			this.viewType = item.questionType;
			var optionsArr = ['', '', '', ''];
			if(item.options) {
				try {
					var optionObject = JSON.parse(item.options);
					optionsArr = Object.values(optionObject);
				} catch(err) {}
			}
			var newitem = JSON.parse(JSON.stringify(item));
			this.topicForm = Object.assign({}, this.topicForm, newitem, {
				optionsArr,
				answers: item.answer ? item.answer.split('') : [],
				answer: item.answer ? item.answer : '',
				userAnswer: item.userAnswer ? item.userAnswer : '',
				userAnswers: item.userAnswer ? item.userAnswer.split('') : [],
			});
			this.$nextTick(() => {
				this.showForm = true;
				this.getTheBtns();
			})
		},
		getTheBtns() {
			var typeIndex = '';
			var viewIndex = this.viewIndex;
			var viewType = this.viewType;
			var questionTypes = this.questionTypes;
			questionTypes.map((item, index) => {
				if(item.value == this.topicForm.questionType) {
					typeIndex = index
				}
			})
			if(viewIndex > 0) {
				this.beforeBtn = true;
			} else {
				var newInfo = this.getBefore(typeIndex, -1);
				if(newInfo && newInfo.hasitem) {
					this.beforeBtn = true;
				} else {
					this.beforeBtn = false;
				}
			}
			if(viewIndex < this[viewType].length - 1) {
				this.afterBtn = true;
			} else {
				var newInfo = this.getBefore(typeIndex, 1);
				if(newInfo && newInfo.hasitem) {
					this.afterBtn = true;
				} else {
					this.afterBtn = false;
				}
			}
		},
		pushList(item) {
			var viewIndex = this.viewIndex;
			var viewType = this.viewType;
			if(viewType == -1 || (viewType != -1 && item.questionType != viewType)) {
				this[item.questionType].push(item);
				if(viewType != -1) {
					this[viewType].splice(viewIndex, 1)
				}
			} else {
				this[item.questionType][viewIndex] = item;
			}
			this.sumSelection();
			this.clearCheck();
		},
		jumpQuestion(type) {
			this.beforeBtn = true;
			this.afterBtn = true;
			var viewIndex = this.viewIndex;
			var viewType = this.viewType;
			var questionTypes = this.questionTypes;
			var typeIndex = '';
			questionTypes.map((item, index) => {
				if(item.value == viewType) {
					typeIndex = index
				}
			})
			if(type < 0) {
				if(viewIndex > 0) {
					viewIndex--;
					this.openList(this[viewType][viewIndex], viewIndex)
				} else {
					var newInfo = this.getBefore(typeIndex, type);
					if(newInfo && newInfo.hasitem) {
						viewIndex = newInfo.theArray.length - 1;
						typeIndex = newInfo.typeIndex;
						this.openList(this[questionTypes[newInfo.typeIndex].value][viewIndex], viewIndex)
					} else {
						this.beforeBtn = false;
					}
				}
			}
			if(type > 0) {
				if(viewIndex < this[viewType].length - 1) {
					viewIndex++;
					this.openList(this[viewType][viewIndex], viewIndex)
				} else {
					var newInfo = this.getBefore(typeIndex, type);
					if(newInfo && newInfo.hasitem) {
						viewIndex = 0;
						typeIndex = newInfo.typeIndex;
						this.openList(this[questionTypes[newInfo.typeIndex].value][viewIndex], viewIndex)
					} else {
						this.afterBtn = false;
					}
				}
			}
		},
		getBefore(typeIndex, type) {
			var questionTypes = this.questionTypes;
			if(type < 0) {
				if(typeIndex <= 0) {
					return {
						typeIndex,
						hasitem: false,
						theArray: [],
					};
				} else {
					typeIndex--;
					var theArray = this[questionTypes[typeIndex].value];
					if(theArray && theArray.length > 0) {
						return {
							typeIndex,
							hasitem: true,
							theArray: theArray,
						};
					} else {
						return this.getBefore(typeIndex, type)
					}
				}
			}
			if(type > 0) {
				if(typeIndex >= questionTypes.length - 1) {
					return {
						typeIndex,
						hasitem: false,
						theArray: [],
					};
				} else {
					typeIndex++;
					var theArray = this[questionTypes[typeIndex].value];
					if(theArray && theArray.length > 0) {
						return {
							typeIndex,
							hasitem: true,
							theArray: theArray,
						};
					} else {
						return this.getBefore(typeIndex, type)
					}
				}
			}
		},
	}
}